package com.uzai.console.service.pyq.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.UzaiRespVo;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.device.DeviceQuery;
import com.uzai.console.dto.feign.turnlink.SimpleTurnLinkDto;
import com.uzai.console.dto.pyq.hdkgoods.PyqHdkGoodsQuery;
import com.uzai.console.dto.pyq.pyqdeviceplan.PyqDevicePlanDto;
import com.uzai.console.dto.pyq.pyqdeviceplan.PyqDevicePlanIdDto;
import com.uzai.console.dto.pyq.pyqdeviceplan.PyqDevicePlanQuery;
import com.uzai.console.dto.pyq.pyqmaterial.TurnlinkDto;
import com.uzai.console.dto.wechatprivate.devicewechatinfo.DeviceWechatInfoQuery;
import com.uzai.console.dto.wechatwork.devicewxworkinfo.DeviceWxworkInfoQuery;
import com.uzai.console.entity.*;
import com.uzai.console.mapper.*;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.feign.UzaiTurnlinkFeignService;
import com.uzai.console.service.pyq.PyqDevicePlanService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.device.DeviceInfoVo;
import com.uzai.console.vo.feign.turnlink.SimpleTurnlinkVo;
import com.uzai.console.vo.hdk.HdkGoods;
import com.uzai.console.vo.pyq.pyqdeviceplan.PyqDevicePlanVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.support.collections.DefaultRedisList;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 朋友圈设备方案
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class PyqDevicePlanServiceImpl implements PyqDevicePlanService {

    Logger logger = LoggerFactory.getLogger(PyqDevicePlanServiceImpl.class);

    @Autowired
    private PyqDevicePlanMapper pyqDevicePlanMapper;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private UzaiTurnlinkFeignService uzaiTurnlinkFeignService;
    @Autowired
    private DeviceWechatInfoMapper deviceWechatInfoMapper;
    @Autowired
    private DeviceWxworkInfoMapper deviceWxworkInfoMapper;
    @Autowired
    private ConfigPlanMapper configPlanMapper;
    @Autowired
    private TbTokenMapper tbTokenMapper;
    @Autowired
    private PyqHdkGoodsMapper pyqHdkGoodsMapper;

    /**
     * 查询朋友圈设备方案列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqDevicePlanQuery
     * @return null
     */
    public Page<PyqDevicePlanVo> findPyqDevicePlanList(PyqDevicePlanQuery pyqDevicePlanQuery){
        //新建返回到页面的LIST对象
        List<PyqDevicePlanVo> pyqDevicePlanPageVoList = new ArrayList<>();

        //执行分页查询对象
        Page<PyqDevicePlan> page = new Page<>();
        pyqDevicePlanQuery.convert(page);
        ArrayList<PyqDevicePlan> pyqDevicePlanList =  pyqDevicePlanMapper.findByList(page, pyqDevicePlanQuery);
        if(pyqDevicePlanList != null && pyqDevicePlanList.size() > 0){
            for (PyqDevicePlan pyqDevicePlan : pyqDevicePlanList){
                PyqDevicePlanVo pyqDevicePlanPageVo = new PyqDevicePlanVo();
                BeanUtils.copyProperties(pyqDevicePlan, pyqDevicePlanPageVo);

                //获取机器人信息
                List<DeviceInfoVo> deviceInfoVoList = new ArrayList<>();
                List<Long> deviceUniqueIdList = JSONObject.parseArray(pyqDevicePlan.getDeviceInfo(), Long.class);
                if(deviceUniqueIdList != null && deviceUniqueIdList.size() > 0){
                    for (Long deviceUniqueId : deviceUniqueIdList){
                        Device device = deviceService.findDeviceInfoByDeviceUniqueId(deviceUniqueId, pyqDevicePlan.getDeviceType(), pyqDevicePlan.getMerId());
                        if(device != null){
                            DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                            BeanUtils.copyProperties(device, deviceInfoVo);
                            deviceInfoVoList.add(deviceInfoVo);
                        }
                    }
                }
                pyqDevicePlanPageVo.setDeviceList(deviceInfoVoList);
                pyqDevicePlanPageVoList.add(pyqDevicePlanPageVo);
            }
        }

        //返回到页面分页对象
        Page<PyqDevicePlanVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(pyqDevicePlanPageVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询朋友圈设备方案详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqDevicePlanIdDto
     * @return null
     */
    public PyqDevicePlanVo findPyqDevicePlan(PyqDevicePlanIdDto pyqDevicePlanIdDto){
        if(pyqDevicePlanIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        PyqDevicePlan pyqDevicePlan = pyqDevicePlanMapper.selectById(pyqDevicePlanIdDto.getId(), pyqDevicePlanIdDto.getMerId());

        if(pyqDevicePlan == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该朋友圈设备方案不存在");
        }

        PyqDevicePlanVo pyqDevicePlanVo = new PyqDevicePlanVo();
        BeanUtils.copyProperties(pyqDevicePlan, pyqDevicePlanVo);

        //获取机器人信息
        List<DeviceInfoVo> deviceInfoVoList = new ArrayList<>();
        List<Long> deviceUniqueIdList = JSONObject.parseArray(pyqDevicePlan.getDeviceInfo(), Long.class);
        if(deviceUniqueIdList != null && deviceUniqueIdList.size() > 0){
            for (Long deviceUniqueId : deviceUniqueIdList){
                Device device = deviceService.findDeviceInfoByDeviceUniqueId(deviceUniqueId, pyqDevicePlan.getDeviceType(), pyqDevicePlan.getMerId());
                if(device != null){
                    DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                    BeanUtils.copyProperties(device, deviceInfoVo);
                    deviceInfoVoList.add(deviceInfoVo);
                }
            }
        }
        pyqDevicePlanVo.setDeviceList(deviceInfoVoList);

        return pyqDevicePlanVo;
    }

    /**
     * 添加朋友圈设备方案
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqDevicePlanDto
     * @return null
     */
    public void addPyqDevicePlan(PyqDevicePlanDto pyqDevicePlanDto){

        if(StringUtils.isBlank(pyqDevicePlanDto.getName())){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入方案名称");
        }

        if(pyqDevicePlanDto.getTokenType() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择联盟设置类型");
        }

        if(pyqDevicePlanDto.getDeviceModel() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择发送机器人模式");
        }

        if(StringUtils.isBlank(pyqDevicePlanDto.getDeviceType())){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择设备类型");
        }

        //测试发送机器人唯一id
//        Device device = null;
//        if(pyqDevicePlanDto.getDeviceModel().intValue() == 0){ //发送部分机器人模式
//            //验证发送机器人
//            if(pyqDevicePlanDto.getDeviceList() == null || pyqDevicePlanDto.getDeviceList().size() == 0){
//                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择发送机器人");
//            }
//            device = deviceService.findDeviceInfoByDeviceUniqueId(pyqDevicePlanDto.getDeviceList().get(0), pyqDevicePlanDto.getDeviceType(), pyqDevicePlanDto.getMerId());
//        }else{ //全部机器人，则去查询一个第一个机器人测试用
//
//            //个人微信
//            if(pyqDevicePlanDto.getDeviceType().equals(DeviceTypeEnum.DT_PC_WECHAT.getKey())){
//                Page<DeviceWechatInfo> page = new Page<>();
//                DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
//                deviceWechatInfoQuery.setMerId(pyqDevicePlanDto.getMerId());
//                List<DeviceWechatInfo> deviceWechatInfoList = deviceWechatInfoMapper.findByList(page, deviceWechatInfoQuery);
//                if(deviceWechatInfoList == null || deviceWechatInfoList.size() == 0 ){
//                    throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "该运营商没有个人微信机器人");
//                }
//                //默认取第一个测试用
//                DeviceWechatInfo deviceWechatInfo = deviceWechatInfoList.get(0);
//                device = new Device();
//                BeanUtils.copyProperties(deviceWechatInfo, device);
//             //企业微信
//            }else if(pyqDevicePlanDto.getDeviceType().equals(DeviceTypeEnum.DT_WORK_WECHAT.getKey())){
//                Page<DeviceWxworkInfo> page = new Page<>();
//                DeviceWxworkInfoQuery deviceWxworkInfoQuery = new DeviceWxworkInfoQuery();
//                deviceWxworkInfoQuery.setMerId(pyqDevicePlanDto.getMerId());
//                List<DeviceWxworkInfo> deviceWxworkInfoList = deviceWxworkInfoMapper.findByList(page, deviceWxworkInfoQuery);
//                if(deviceWxworkInfoList == null || deviceWxworkInfoList.size() == 0 ){
//                    throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "该运营商没有企业微信机器人");
//                }
//                //默认取第一个测试用
//                DeviceWxworkInfo deviceWxworkInfo = deviceWxworkInfoList.get(0);
//                device = new Device();
//                BeanUtils.copyProperties(deviceWxworkInfo, device);
//            }
//        }
//
//        if(device == null){
//            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "系统未找到任何机器人信息");
//        }
//
//        //先转链
//        Long tbTokenId = null;
//        String pid = "";
//        if(pyqDevicePlanDto.getTokenType() != null && pyqDevicePlanDto.getTokenType().intValue() == 1){ //自己设置联盟
//            JSONObject jsonObjectToken = JSONObject.parseObject(pyqDevicePlanDto.getTokenInfo());
//            if(jsonObjectToken != null){
//                //淘宝token
//                JSONObject tbTokenJsonObject  = jsonObjectToken.getJSONObject("tbToken");
//                if(tbTokenJsonObject != null){
//                    tbTokenId = tbTokenJsonObject.getLong("tokenId");
//                    pid = tbTokenJsonObject.getString("pid");
//                }
//            }
//        }else{ //默认从机器人中获取
//            ConfigPlan configPlan = configPlanMapper.selectById(device.getConfigId(), device.getMerId());
//            if(configPlan != null){
//                //淘宝token
//                JSONObject jsonObject_tb = JSONObject.parseObject(configPlan.getTbAccountId());
//                if(jsonObject_tb != null) {
//                    JSONArray jsonArray = jsonObject_tb.getJSONArray("tokens");
//                    if (jsonArray != null && jsonArray.size() > 0) {
//                        JSONObject jsonObject = (JSONObject) jsonArray.get(0);
//                        String tokenIds = jsonObject.getString("tokenIds");
//                        List<Long> tokenList = JSONObject.parseArray(tokenIds, Long.class);
//                        if (tokenList != null && tokenList.size() > 0) {
//                            tbTokenId = tokenList.get(0);
//                            TbToken tbToken = tbTokenMapper.selectById(tbTokenId, pyqDevicePlanDto.getMerId());
//                            if (tbToken != null) {
//                                pid = tbToken.getDefPid();
//                            }
//                        }
//                    }
//                }
//            }
//        }
//
//        //测试能否正常转链
//        SimpleTurnLinkDto simpleTurnLinkDto = new  SimpleTurnLinkDto();
//        //判断输入得是商品id还是商品链接
//        //从大淘客查询最新的商品
//        Page<PyqHdkGoods> page = new Page<>();
//        PyqHdkGoodsQuery pyqHdkGoodsQuery = new PyqHdkGoodsQuery();
//        PyqHdkGoods pyqHdkGoods = pyqHdkGoodsMapper.selectNewest(null, null);
//        if(pyqHdkGoods != null){
//            simpleTurnLinkDto.setItemId(pyqHdkGoods.getItemid());
//            simpleTurnLinkDto.setMerId(pyqDevicePlanDto.getMerId());
//            simpleTurnLinkDto.setGenerateShortUrl(1);
//            simpleTurnLinkDto.setMiddlePageDomainId(pyqDevicePlanDto.getDomain());
//            simpleTurnLinkDto.setTokenId(tbTokenId);
//            simpleTurnLinkDto.setPid(pid);
//            UzaiRespVo<SimpleTurnlinkVo> uzaiRespVo = uzaiTurnlinkFeignService.tbTurnlinkSimple(simpleTurnLinkDto);
//            if (uzaiRespVo.getCode() != 200) {
//                throw new BusinessException(uzaiRespVo.getMsg());
//            }
//        }

        Integer now = DateUtil.getNowTime();
        PyqDevicePlan pyqDevicePlan = new PyqDevicePlan();
        pyqDevicePlan.setId(IdWorker.getId());
        pyqDevicePlan.setMerId(pyqDevicePlanDto.getMerId());
        pyqDevicePlan.setDeviceType(pyqDevicePlanDto.getDeviceType());
        pyqDevicePlan.setName(pyqDevicePlanDto.getName());
        pyqDevicePlan.setDeviceModel(pyqDevicePlanDto.getDeviceModel());
        pyqDevicePlan.setDeviceInfo(Tools.getStrEmpty(JSONObject.toJSONString(pyqDevicePlanDto.getDeviceList())));
        pyqDevicePlan.setTokenType(pyqDevicePlanDto.getTokenType());
        pyqDevicePlan.setTokenInfo(Tools.getStr(pyqDevicePlanDto.getTokenInfo()));
        pyqDevicePlan.setDomain(Tools.getLong(pyqDevicePlanDto.getDomain()));
        pyqDevicePlan.setCreateTime(now);
        pyqDevicePlan.setUpdateTime(now);
        pyqDevicePlanMapper.insert(pyqDevicePlan);
    }

    /**
     * 修改朋友圈设备方案
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqDevicePlanDto
     * @return null
     */
    public void updatePyqDevicePlan(PyqDevicePlanDto pyqDevicePlanDto){
        if(pyqDevicePlanDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        if(StringUtils.isBlank(pyqDevicePlanDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入方案名称");
        }

        if(pyqDevicePlanDto.getTokenType() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择联盟设置类型");
        }

        if(pyqDevicePlanDto.getDeviceModel() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择发送机器人模式");
        }

        //测试发送机器人唯一id
//        Device device = null;
//        if(pyqDevicePlanDto.getDeviceModel().intValue() == 0){ //发送部分机器人模式
//            //验证发送机器人
//            if(pyqDevicePlanDto.getDeviceList() == null || pyqDevicePlanDto.getDeviceList().size() == 0){
//                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择发送机器人");
//            }
//            device = deviceService.findDeviceInfoByDeviceUniqueId(pyqDevicePlanDto.getDeviceList().get(0), pyqDevicePlanDto.getDeviceType(), pyqDevicePlanDto.getMerId());
//        }else{ //全部机器人，则去查询一个第一个机器人测试用
//            //个人微信
//            if(pyqDevicePlanDto.getDeviceType().equals(DeviceTypeEnum.DT_PC_WECHAT.getKey())){
//                Page<DeviceWechatInfo> page = new Page<>();
//                DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
//                deviceWechatInfoQuery.setMerId(pyqDevicePlanDto.getMerId());
//                List<DeviceWechatInfo> deviceWechatInfoList = deviceWechatInfoMapper.findByList(page, deviceWechatInfoQuery);
//                if(deviceWechatInfoList == null || deviceWechatInfoList.size() == 0 ){
//                    throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "该运营商没有个人微信机器人");
//                }
//                //默认取第一个测试用
//                DeviceWechatInfo deviceWechatInfo = deviceWechatInfoList.get(0);
//                device = new Device();
//                BeanUtils.copyProperties(deviceWechatInfo, device);
//             //企业微信
//            }else if(pyqDevicePlanDto.getDeviceType().equals(DeviceTypeEnum.DT_WORK_WECHAT.getKey())){
//                Page<DeviceWxworkInfo> page = new Page<>();
//                DeviceWxworkInfoQuery deviceWxworkInfoQuery = new DeviceWxworkInfoQuery();
//                deviceWxworkInfoQuery.setMerId(pyqDevicePlanDto.getMerId());
//                List<DeviceWxworkInfo> deviceWxworkInfoList = deviceWxworkInfoMapper.findByList(page, deviceWxworkInfoQuery);
//                if(deviceWxworkInfoList == null || deviceWxworkInfoList.size() == 0 ){
//                    throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "该运营商没有企业微信机器人");
//                }
//                //默认取第一个测试用
//                DeviceWxworkInfo deviceWxworkInfo = deviceWxworkInfoList.get(0);
//                device = new Device();
//                BeanUtils.copyProperties(deviceWxworkInfo, device);
//            }
//        }
//
//        if(device == null){
//            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "系统未找到任何机器人信息");
//        }
//
//        //先转链
//        Long tbTokenId = null;
//        String pid = "";
//        if(pyqDevicePlanDto.getTokenType() != null && pyqDevicePlanDto.getTokenType().intValue() == 1){ //自己设置联盟
//            JSONObject jsonObjectToken = JSONObject.parseObject(pyqDevicePlanDto.getTokenInfo());
//            if(jsonObjectToken != null){
//                //淘宝token
//                JSONObject tbTokenJsonObject  = jsonObjectToken.getJSONObject("tbToken");
//                if(tbTokenJsonObject != null){
//                    tbTokenId = tbTokenJsonObject.getLong("tokenId");
//                    pid = tbTokenJsonObject.getString("pid");
//                }
//            }
//        }else{ //默认从机器人中获取
//            ConfigPlan configPlan = configPlanMapper.selectById(device.getConfigId(), device.getMerId());
//            if(configPlan != null){
//                //淘宝token
//                JSONObject jsonObject_tb = JSONObject.parseObject(configPlan.getTbAccountId());
//                if(jsonObject_tb != null) {
//                    JSONArray jsonArray = jsonObject_tb.getJSONArray("tokens");
//                    if (jsonArray != null && jsonArray.size() > 0) {
//                        JSONObject jsonObject = (JSONObject) jsonArray.get(0);
//                        String tokenIds = jsonObject.getString("tokenIds");
//                        List<Long> tokenList = JSONObject.parseArray(tokenIds, Long.class);
//                        if (tokenList != null && tokenList.size() > 0) {
//                            tbTokenId = tokenList.get(0);
//                            TbToken tbToken = tbTokenMapper.selectById(tbTokenId, pyqDevicePlanDto.getMerId());
//                            if (tbToken != null) {
//                                pid = tbToken.getDefPid();
//                            }
//                        }
//                    }
//                }
//            }
//        }
//
//        //测试能否正常转链
//        SimpleTurnLinkDto simpleTurnLinkDto = new  SimpleTurnLinkDto();
//        //判断输入得是商品id还是商品链接
//        //从大淘客查询最新的商品
//        Page<PyqHdkGoods> page = new Page<>();
//        PyqHdkGoodsQuery pyqHdkGoodsQuery = new PyqHdkGoodsQuery();
//        PyqHdkGoods pyqHdkGoods = pyqHdkGoodsMapper.selectNewest(null, null);
//        if(pyqHdkGoods != null){
//            simpleTurnLinkDto.setItemId(pyqHdkGoods.getItemid());
//            simpleTurnLinkDto.setMerId(pyqDevicePlanDto.getMerId());
//            simpleTurnLinkDto.setGenerateShortUrl(1);
//            simpleTurnLinkDto.setMiddlePageDomainId(pyqDevicePlanDto.getDomain());
//            simpleTurnLinkDto.setTokenId(tbTokenId);
//            simpleTurnLinkDto.setPid(pid);
//            UzaiRespVo<SimpleTurnlinkVo> uzaiRespVo = uzaiTurnlinkFeignService.tbTurnlinkSimple(simpleTurnLinkDto);
//            if (uzaiRespVo.getCode() != 200) {
//                throw new BusinessException(uzaiRespVo.getMsg());
//            }
//        }

        PyqDevicePlan pyqDevicePlan = pyqDevicePlanMapper.selectById(pyqDevicePlanDto.getId(), pyqDevicePlanDto.getMerId());
        if(pyqDevicePlan == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该设备方案不存在");
        }

        pyqDevicePlan.setName(pyqDevicePlanDto.getName());
        pyqDevicePlan.setDeviceModel(pyqDevicePlanDto.getDeviceModel());
        pyqDevicePlan.setDeviceInfo(Tools.getStrEmpty(JSONObject.toJSONString(pyqDevicePlanDto.getDeviceList())));
        pyqDevicePlan.setDomain(Tools.getLong(pyqDevicePlanDto.getDomain()));
        pyqDevicePlan.setTokenType(pyqDevicePlanDto.getTokenType());
        if(pyqDevicePlanDto.getTokenType().intValue() == 1){ //自定义联盟信息
            pyqDevicePlan.setTokenInfo(Tools.getStr(pyqDevicePlanDto.getTokenInfo()));
        }
        Integer now = DateUtil.getNowTime();
        pyqDevicePlan.setUpdateTime(now);
        pyqDevicePlanMapper.updateById(pyqDevicePlan);
    }

    /**
     * 删除
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqDevicePlanIdDto
     * @return null
     */
    public void deletePyqDevicePlan(PyqDevicePlanIdDto pyqDevicePlanIdDto){
        if(pyqDevicePlanIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }
        // TODO: 2022/3/8  查询该设备方案是否已经给朋友圈占用了
        pyqDevicePlanMapper.deleteById(pyqDevicePlanIdDto.getId(), pyqDevicePlanIdDto.getMerId());
    }

    /**
     * 查询朋友圈设备方案下拉列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param query
     * @return null
     */
    public List<SelectKeyVo> findSelectList(PyqDevicePlanQuery query){
        List<SelectKeyVo> selectKeyVoList = new ArrayList<>();
        List<PyqDevicePlan> pyqDevicePlanList =  pyqDevicePlanMapper.findByList(query);
        if(pyqDevicePlanList != null && pyqDevicePlanList.size() > 0){
            for (PyqDevicePlan pyqDevicePlan : pyqDevicePlanList){
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(pyqDevicePlan.getId()));
                selectKeyVo.setValue(pyqDevicePlan.getName());
                selectKeyVoList.add(selectKeyVo);
            }
        }
        return selectKeyVoList;
    }

    /**
     * 检测设备方案是否能正常转链
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqDevicePlan
     * @return null
     */
    public boolean validatePyqDevicePlan(PyqDevicePlan pyqDevicePlan){

        //发送朋友圈的机器人列表
        List<Long> deviceUniqueIdList = null;
        if(pyqDevicePlan.getDeviceModel().intValue() == 0){ //发送部分机器人模式
            //验证发送机器人
            deviceUniqueIdList = JSONObject.parseArray(pyqDevicePlan.getDeviceInfo(), Long.class);
        }else if(pyqDevicePlan.getDeviceModel().intValue() ==1){ //全部机器人
            DeviceQuery deviceQuery = new DeviceQuery();
            deviceQuery.setMerId(pyqDevicePlan.getMerId());
            deviceQuery.setDeviceType(pyqDevicePlan.getDeviceType());
            Page<Device> devicePage = deviceService.findDevicePage(deviceQuery);
            if(devicePage == null || devicePage.getRecords() == null || devicePage.getRecords().size() == 0 ){
                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "该设备方案没有机器人");
            }
            deviceUniqueIdList = new ArrayList<>();
            //默认取第一个测试用
            deviceUniqueIdList.add(devicePage.getRecords().get(0).getId());
        }

        if(deviceUniqueIdList == null || deviceUniqueIdList.size() == 0){
            logger.info("该设备方案未选择机器人");
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该设备方案未选择机器人");
        }

        //默认默认第一个机器人
        Long deviceUniqueId = deviceUniqueIdList.get(0);

        Device device = deviceService.findDeviceInfoByDeviceUniqueId(deviceUniqueId, pyqDevicePlan.getDeviceType(), pyqDevicePlan.getMerId());
        if(device == null ){
            logger.info("请求商品转链-机器人id为空");
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "机器人id为空");
        }

        //先转链
        Long tbTokenId = null;
        String pid = "";
        if(pyqDevicePlan.getTokenType() != null && pyqDevicePlan.getTokenType().intValue() == 1){ //自己设置联盟
            JSONObject jsonObjectToken = JSONObject.parseObject(pyqDevicePlan.getTokenInfo());
            if(jsonObjectToken != null){
                //淘宝token
                JSONObject tbTokenJsonObject  = jsonObjectToken.getJSONObject("tbToken");
                if(tbTokenJsonObject != null){
                    tbTokenId = tbTokenJsonObject.getLong("tokenId");
                    pid = tbTokenJsonObject.getString("pid");
                }
            }
        }else{ //默认从机器人中获取
            ConfigPlan configPlan = configPlanMapper.selectById(device.getConfigId(), device.getMerId());
            if(configPlan != null){
                //淘宝token
                JSONObject jsonObject_tb = JSONObject.parseObject(configPlan.getTbAccountId());
                if(jsonObject_tb != null) {
                    JSONArray jsonArray = jsonObject_tb.getJSONArray("tokens");
                    if (jsonArray != null && jsonArray.size() > 0) {
                        JSONObject jsonObject = (JSONObject) jsonArray.get(0);
                        String tokenIds = jsonObject.getString("tokenIds");
                        List<Long> tokenList = JSONObject.parseArray(tokenIds, Long.class);
                        if (tokenList != null && tokenList.size() > 0) {
                            tbTokenId = tokenList.get(0);
                            TbToken tbToken = tbTokenMapper.selectById(tbTokenId, pyqDevicePlan.getMerId());
                            if (tbToken != null) {
                                pid = tbToken.getDefPid();
                            }
                        }
                    }
                }
            }
        }

        //测试能否正常转链
        SimpleTurnLinkDto simpleTurnLinkDto = new  SimpleTurnLinkDto();
        //判断输入得是商品id还是商品链接
        //从大淘客查询最新的商品
        Page<PyqHdkGoods> page = new Page<>();
        PyqHdkGoodsQuery pyqHdkGoodsQuery = new PyqHdkGoodsQuery();
        PyqHdkGoods pyqHdkGoods = pyqHdkGoodsMapper.selectNewest(null, null);
        if(pyqHdkGoods != null){
            simpleTurnLinkDto.setItemId(pyqHdkGoods.getItemid());
            simpleTurnLinkDto.setMerId(pyqDevicePlan.getMerId());
            simpleTurnLinkDto.setGenerateShortUrl(0);
            simpleTurnLinkDto.setMiddlePageDomainId(pyqDevicePlan.getDomain());
            simpleTurnLinkDto.setTokenId(tbTokenId);
            simpleTurnLinkDto.setPid(pid);
            UzaiRespVo<SimpleTurnlinkVo> uzaiRespVo = uzaiTurnlinkFeignService.tbTurnlinkSimple(simpleTurnLinkDto);
            if(uzaiRespVo != null && uzaiRespVo.getCode() == 200){
                return true;
            }else{
                throw new BusinessException(uzaiRespVo.getMsg());
            }
        }

        return false;
    }
}
